package com.example.jbservice.Service.Impl;

import com.example.jbservice.Bean.Register;
import com.example.jbservice.Dto.DeptDto;
import com.example.jbservice.Dto.PatientDto;
import com.example.jbservice.Dto.RegisterDTO;
import com.example.jbservice.Dto.YQDto;
import com.example.jbservice.Mapper.RegisterMapper;
import com.example.jbservice.Service.RegisterService;
import com.example.jbservice.Service.YQService;
import com.example.jbservice.VO.PatientVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private YQService yqService;
    @Override
    @Transactional
    public void Insert(RegisterDTO registerDTO) {
            Register register = new Register();
            BeanUtils.copyProperties(registerDTO, register);
            register.setOper_date(LocalDateTime.now());
            register.setReg_date(LocalDateTime.now());
            registerMapper.insert(register);
            YQDto yqDto = new YQDto();
            BeanUtils.copyProperties(registerDTO, yqDto);
            yqService.Insert(yqDto);

    }

    @Override
    public Map<String,String> SearchDept() {
        log.info("RegisterServiceImpl查询科室字典");
        return  registerMapper.SearchDept();
    }

    @Override
    public List<DeptDto> GetDept() {
        return registerMapper.GetDept();
    }

    @Override
    public String Qry() {
        return registerMapper.Qry();
    }

    @Override
    public String GetPatientCardByIdenno(String idenno) {
        return registerMapper.GetPatientCardByIdenno(idenno);
    }

    @Override
    public PatientDto GetPatientInfoByCardNo(String card_no) {

        return registerMapper.GetPatientInfoByCardNo(card_no);
    }

    @Override
    public void CreatePatient(PatientDto patientDto) {
        registerMapper.CreatePatient(patientDto);
    }

    @Override
    @Transactional
    public void cancleReg(RegisterDTO registerDTO) {
        //1.校验挂号数据是否有效i

        Integer i=registerMapper.getRegisterValid(registerDTO.getClinic_code());
        if(i==0){
            throw  new RuntimeException("未找到有效的挂号信息！");
        }


        //作废挂号表
        registerMapper.cancleReg(registerDTO);

        //插一条退费的缴费中间表
        YQDto yq=new YQDto();
        yq.setClinic_code(registerDTO.getClinic_code());
        yq.setTrade_no(registerDTO.getTrade_no());
        yqService.Insert(yq);

    }
}
